home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dme / main.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  23KB  |  1,099 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  * MAIN.C
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13. #include <exec/tasks.h>
  14. #include <workbench/startup.h>
  15. #include <workbench/workbench.h>
  16. #include <libraries/asl.h>
  17. #include <proto/asl.h>
  18. #include <graphics/gfxbase.h>
  19. #include <graphics/rastport.h>
  20.  
  21. #if AREXX
  22. #include <lib/rexx.h>
  23. #endif
  24.  
  25. Prototype short Xsize, Ysize;
  26. Prototype short XTbase, YTbase;
  27. Prototype short Rows, Columns;
  28. Prototype short Xbase, Ybase;
  29. Prototype short Xpixs, Ypixs;
  30.  
  31. Prototype ubyte *av[];
  32. Prototype struct FileRequester *FReq;
  33. Prototype char *RexxHostName;
  34. Prototype short IconSaveOpt;
  35. Prototype struct WBStartup *Wbs;
  36. Prototype struct Library *IconBase;
  37.  
  38.  
  39. Prototype void ipchandler (void);
  40. Prototype void initipc (void);
  41. Prototype void do_ipc (void);
  42. Prototype void do_iconify (void);
  43. Prototype void do_iconsave(void);
  44. Prototype void do_tomouse (void);
  45. Prototype void iconify (void);
  46. Prototype void uniconify (void);
  47. Prototype void do_newwindow (void);
  48. Prototype void do_openwindow(void);
  49. Prototype struct Window *TOpenWindow (struct NewWindow *);
  50. Prototype struct Window *opensharedwindow (struct NewWindow *);
  51. Prototype void closesharedwindow (struct Window *);
  52. Prototype int getyn (char *);
  53. Prototype void title (char *);
  54. Prototype void window_title (void);
  55. Prototype void set_window_params (void);
  56. Prototype void exiterr (char *);
  57. Prototype int breakcheck (void);
  58. Prototype void breakreset (void);
  59. Prototype void do_resize (void);
  60. Prototype int ops (char **, int);
  61. Prototype char *geoskip(char *, int *, int *);
  62. Prototype void GeometryToNW(char *, struct NewWindow *);
  63.  
  64. typedef struct Process        PROC;
  65. typedef struct WBStartup    WBS;
  66. typedef struct DiskObject   DISKOBJ;
  67.  
  68. #define IDCMPFLAGS   CLOSEWINDOW|NEWSIZE|RAWKEY|MOUSEBUTTONS|ACTIVEWINDOW|\
  69.              MOUSEMOVE|MENUPICK
  70.  
  71. struct NewWindow Nw = {
  72.    0, 1, 0  , 0  , -1, -1,  /*    width, height filled in by program */
  73.    IDCMPFLAGS,
  74.    ACTIVATE|WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|RMBTRAP,
  75.    NULL, NULL, (ubyte *)"   WAIT   ",
  76.    NULL, NULL,
  77.    32, 32, -1, -1,
  78.    WBENCHSCREEN
  79. };
  80.  
  81. short Sharedrefs;
  82. short Oldtlen = 999;      /*  Old Title Length      */
  83. struct MsgPort *Sharedport;
  84. struct FileRequester *FReq;
  85. static DISKOBJ *Do;
  86. WBS    *Wbs;
  87.  
  88. short Xsize,  Ysize;        /* font character sizes        */
  89. short Rows,  Columns;        /* character rows/cols available       */
  90. short Xbase,  Ybase;        /* offset pixel base for display       */
  91. short XTbase,YTbase;        /* used for text display           */
  92. short Xpixs,  Ypixs;        /* actual # X/Y pixels available       */
  93. short Mx, My;
  94. short IconSaveOpt;
  95.  
  96. ubyte *av[8];
  97. char Quitflag;
  98. char Overide;
  99. char MShowTitle, MForceTitle;
  100.  
  101. char    *RexxHostName = "DME";
  102.  
  103.  
  104. long Mask;
  105.  
  106. struct IntuitionBase *IntuitionBase;
  107. struct GfxBase *GfxBase;
  108. struct Library *IconBase;
  109. struct Library *AslBase;
  110.  
  111. extern int Enable_Abort;
  112.  
  113. static char *Ffile;
  114.  
  115. int _bufsiz = 8192;
  116.  
  117. int main(int, char **);
  118. int mybrk(void);
  119.  
  120. int
  121. wbmain(wbs)
  122. WBS *wbs;
  123. {
  124.     IconSaveOpt = 1;
  125.     return(main(0, (char **)wbs));
  126. }
  127.  
  128. int
  129. main(mac, mav)
  130. int mac;
  131. char **mav;
  132. {
  133.     char nf, ni;        /*    # files on command line     */
  134.     char notdone;        /*    for endless loop        */
  135.     char iawm = 0;        /*    overide mouse buttons        */
  136.     char dontwait = 0;        /*    don't wait for a message    */
  137.     short i;
  138.     short Code;
  139.     PROC *proc = (PROC *)FindTask(NULL);
  140.     BPTR origlock;
  141.  
  142.     onbreak(mybrk);
  143.     fclose(stdin);
  144.     /*fclose(stdout);*/     /*    assume person will run >nil:    */
  145.     fclose(stderr);        /*    close stderr & console ref. */
  146.     origlock = CurrentDir(DupLock((BPTR)proc->pr_CurrentDir));
  147.  
  148.     NewList((LIST *)&DBase);
  149.     NewList((LIST *)&PBase);
  150.     IntuitionBase = (void *)OpenLibrary("intuition.library", 0);
  151.     GfxBase = (void *)OpenLibrary("graphics.library", 0);
  152.     if (IntuitionBase == (void *)NULL || GfxBase == (void *)NULL)
  153.     exiterr("cannot open intuition or graphics library");
  154.     if (AslBase = OpenLibrary("asl.library", 0))
  155.     FReq = AllocAslRequest(ASL_FileRequest, NULL);
  156.     IconBase = OpenLibrary("icon.library", 0);
  157.  
  158.     init_command();
  159.  
  160.     String  = (char *)malloc(1);    /*  initialize scanf variable    */
  161.     *String = 0;
  162.  
  163.     if (mac == 0) {        /*  WORKBENCH STARTUP    */
  164.     Wbs = (WBS *)mav;
  165.     if (IconBase == NULL)
  166.         exiterr("unable to open icon library");
  167.     }
  168.  
  169.     resethash();
  170.  
  171.     if (Wbs) {
  172.     if (Wbs->sm_ArgList[0].wa_Lock) {
  173.         BPTR savelock = CurrentDir((BPTR)Wbs->sm_ArgList[0].wa_Lock);
  174.         if (Do = GetDiskObject(Wbs->sm_ArgList[0].wa_Name)) {
  175.         ops(Do->do_ToolTypes, 1);
  176.         FreeDiskObject(Do);
  177.         }
  178.         CurrentDir(savelock);
  179.     }
  180.     nf = Wbs->sm_NumArgs - 1;
  181.     mac = 99;
  182.     } else {
  183.     nf = ops(mav+1, 0);
  184.     }
  185.  
  186.     for (ni = 0, i = 1; i < mac; ++i) {
  187.     char *str;
  188.     DISKOBJ *dso;
  189.     if (Wbs) {
  190.         if (i > nf)
  191.         break;
  192.         str = Wbs->sm_ArgList[i].wa_Name;
  193.         UnLock(CurrentDir(DupLock((BPTR)Wbs->sm_ArgList[i].wa_Lock)));
  194.         if (dso = GetDiskObject(Wbs->sm_ArgList[i].wa_Name)) {
  195.         ops(dso->do_ToolTypes, 1);
  196.         FreeDiskObject(dso);
  197.         }
  198.     } else {
  199.         str = mav[i];
  200.         if (*str == '-')
  201.         continue;
  202.     }
  203.     do_newwindow();
  204.     ++ni;
  205.     av[0] = (ubyte *)"newfile";
  206.     av[1] = (ubyte *)str;
  207.     do_edit();
  208.     MForceTitle = 1;
  209.     window_title();
  210.     }
  211.     if (nf == 0)            /* no files to edit */
  212.     do_newwindow();
  213.  
  214. #if AREXX
  215.     init_arexx();
  216. #endif
  217.  
  218.     mountrequest(0);
  219.     av[0] = NULL;
  220.     av[1] = (ubyte *)"s:.edrc";
  221.     do_source();
  222.     av[0] = NULL;
  223.     av[1] = (ubyte *)((Ffile) ? Ffile : ".edrc");
  224.     do_source();
  225.     mountrequest(1);
  226.     {                /*    1.29c    */
  227.     ED *ep;
  228.     ED *eb = Ep;
  229.     if (eb) {
  230.         for (ep = (ED *)eb->Node.mln_Succ; ep->Node.mln_Succ; ep = (ED *)ep->Node.mln_Succ) {
  231.         ep->Tabstop = eb->Tabstop;
  232.         ep->Margin  = eb->Margin;
  233.         ep->Insertmode = eb->Insertmode;
  234.         ep->IgnoreCase = eb->IgnoreCase;
  235.         ep->Wordwrap   = eb->Wordwrap;
  236.         if (eb->Font) {
  237.             ep->Font = eb->Font;
  238.             ++eb->Font->tf_Accessors;
  239.         }
  240.         }
  241.     }
  242.     }
  243.     title("DME V1.50 \251Copyright 1988-1994 by Matthew Dillon,  All Rights Reserved             ");
  244.     Mask |= 1 << Ep->Win->UserPort->mp_SigBit;
  245. #if AREXX
  246.     Mask |= 1 << RexxSigBit;
  247. #endif
  248. loop:
  249.     if (!Ep->iconmode)
  250.     text_cursor(1);
  251.     for (notdone = 1; !Quitflag && notdone;) {
  252.     char mmove = 0;
  253.     short mqual;
  254.     long mask;
  255.  
  256.     if (!Ep->iconmode)
  257.         window_title();
  258.     if (dontwait) {
  259.         --dontwait;
  260.         mask = FindTask(NULL)->tc_SigRecvd;
  261.     } else {
  262.         mask = Wait(Mask);
  263.     }
  264.  
  265.     /*
  266.      *  NOTE: due to operation of breakcheck(), the userport signal
  267.      *  may not be set even if there are messages pending.
  268.      */
  269.  
  270.     {
  271.         IMESS *im;
  272.         while (im = (IMESS *)GetMsg(Ep->Win->UserPort)) {
  273.         Msgchk = 1;
  274.         Abortcommand = 0;
  275.         Code = im->Code;
  276.         if (im->IDCMPWindow != Ep->Win) {
  277.             Overide = 0;
  278.             if (Comlinemode)
  279.             escapecomlinemode();
  280.             text_sync();
  281.             MShowTitle = 0;
  282.             if (!Ep->iconmode)
  283.             window_title();
  284.             if (text_switch(im->IDCMPWindow) == 0) {
  285.             ReplyMsg((MSG *)im);
  286.             continue;
  287.             }
  288.         }
  289.         Mx = im->MouseX;
  290.         My = im->MouseY;
  291.         switch(im->Class) {
  292.         case NEWSIZE:
  293.             if (!Ep->iconmode) {
  294.             if (Comlinemode)
  295.                 escapecomlinemode();
  296.             set_window_params();
  297.             if (!text_sync())
  298.                 text_redisplay();
  299.             text_cursor(1);
  300.             }
  301.             break;
  302.         case MOUSEBUTTONS:
  303.             switch(Code) {
  304.             case SELECTDOWN:
  305.             case MENUDOWN:
  306.             if (Ep->iconmode || iawm) {
  307.                 uniconify();
  308.                 text_cursor(1);
  309.                 break;
  310.             }
  311.             Forbid();
  312.             Ep->Win->Flags |= REPORTMOUSE;
  313.             Permit();
  314.             uniconify();
  315.             text_cursor(0);
  316.             keyctl(NULL, im->Code|0x80, im->Qualifier);
  317.             text_cursor(1);
  318.             break;
  319.             case SELECTUP:
  320.             case MENUUP:
  321.             Forbid();
  322.             Ep->Win->Flags &= ~REPORTMOUSE;
  323.             Permit();
  324.             break;
  325.             }
  326.             break;
  327.         case RAWKEY:
  328.             if ((im->Code & 0x80) == 0) {
  329.             /*  Handled in command interpreter.
  330.             if (Ep->iconmode) {
  331.                 uniconify();
  332.                 break;
  333.             }
  334.             */
  335.             text_cursor(0);
  336.             keyctl(im, im->Code, im->Qualifier);
  337.             text_cursor(1);
  338.             }
  339.             break;
  340.         case MENUPICK:
  341.             {
  342.             char *str = menu_cmd(im);
  343.             if (str) {
  344.                 str = strcpy(malloc(strlen(str)+1), str);
  345.                 text_cursor(0);
  346.                 do_command(str);
  347.                 free(str);
  348.                 text_cursor(1);
  349.             }
  350.             }
  351.             break;
  352.         case CLOSEWINDOW:
  353.             if (Comlinemode)
  354.             escapecomlinemode();
  355.             text_sync();
  356.             notdone = 0;
  357.             break;
  358.         case ACTIVEWINDOW:
  359.             if (!Ep->iconmode)
  360.             iawm = 1;
  361.             break;
  362.         case MOUSEMOVE:
  363.             mmove = 1;
  364.             mqual = im->Qualifier;
  365.             break;
  366.         }
  367.         if (im)
  368.             ReplyMsg((MSG *)im);
  369.         if (notdone == 0 || Quitflag) {
  370.             dontwait = 2;
  371.             goto boom;
  372.         }
  373.         }
  374.     }
  375.     if (mask & (1 << RexxSigBit)) {
  376.         if (!Ep->iconmode)
  377.         text_cursor(0);
  378.         ProcessRexxCommands(NULL);
  379.         if (!Ep->iconmode)
  380.         text_cursor(1);
  381.     }
  382.  
  383.     iawm = 0;
  384.     if (mmove) {
  385.         uniconify();
  386.         mmove = 0;
  387.         text_cursor(0);
  388.         keyctl(NULL, QMOVE, mqual);
  389.         text_cursor(1);
  390.     }
  391.     closesharedwindow(NULL);
  392.     }
  393. boom:
  394.     text_sync();
  395.     if (Ep->Modified && !Overide) {
  396.     uniconify();
  397.     Overide = 1;
  398.     title("*** File has been modified ***");
  399.     Quitflag = 0;
  400.     goto loop;
  401.     }
  402.     SetWindowTitles(Ep->Win, "", (char *)-1);
  403.     {
  404.     WIN *win = Ep->Win;
  405.     text_uninit();
  406.     closesharedwindow(win);
  407.     }
  408.     if (Ep) {
  409.     Quitflag = 0;
  410.     if (!Ep->iconmode)
  411.         set_window_params();
  412.     text_load();
  413.     MShowTitle = 0;
  414.     goto loop;
  415.     }
  416.     closesharedwindow(NULL);
  417.     UnLock(CurrentDir(origlock));
  418.     if (IconBase)
  419.     CloseLibrary((void *)IconBase);
  420.     if (FReq)
  421.     FreeAslRequest(FReq);
  422.     if (AslBase)
  423.     CloseLibrary(AslBase);
  424.     if (GfxBase)
  425.     CloseLibrary((void *)GfxBase);
  426.     if (IntuitionBase)
  427.     CloseLibrary((void *)IntuitionBase);
  428.     IconBase = NULL;
  429.     GfxBase  = NULL;
  430.     IntuitionBase = NULL;
  431.     dealloc_hash();
  432.     return(0);
  433. }
  434.  
  435. int
  436. mybrk()
  437. {
  438.     return(0);
  439. }
  440.  
  441. void
  442. do_iconify()
  443. {
  444.     text_sync();
  445.     if (!Comlinemode)
  446.     iconify();
  447. }
  448.  
  449. void
  450. do_iconsave()
  451. {
  452.     static short LastIconSaveOpt;
  453.  
  454.     if (av[1][0] == 'l' || av[1][0] == 'L') {
  455.     IconSaveOpt = LastIconSaveOpt;
  456.     return;
  457.     }
  458.     LastIconSaveOpt = IconSaveOpt;
  459.     if (av[1][0] == 'y' || av[1][0] == 'Y' || stricmp(av[1], "on") == 0)
  460.     IconSaveOpt = 1;
  461.     else
  462.     IconSaveOpt = 0;
  463.  
  464. }
  465.  
  466. void
  467. do_tomouse()
  468. {
  469.     text_position((Mx-Xbase)/Xsize, (My-Ybase)/Ysize);
  470. }
  471.  
  472. /*
  473.  *  New iconify() routine by fgk.
  474.  */
  475.  
  476. void
  477. iconify()
  478. {
  479.     WIN *newwin;
  480.     ED *ep = Ep;
  481.     WIN *win = ep->Win;
  482.  
  483.     struct IntuiText itxt;        /* To find width of prop fonts */
  484.  
  485.     itxt.ITextFont = Ep->Win->WScreen->Font;    /* Init */
  486.     itxt.NextText = NULL;
  487.  
  488.     if (!ep->iconmode) {
  489.     ep->Winx      = win->LeftEdge;
  490.     ep->Winy      = win->TopEdge;
  491.     ep->Winwidth  = win->Width;
  492.     ep->Winheight = win->Height;
  493.  
  494.     /*Nw.Height = win->RPort->TxHeight + 3;*/
  495.  
  496.  
  497.     if(Ep->Win->WScreen->Font != NULL)
  498.         Nw.Height = Ep->Win->WScreen->Font->ta_YSize + 3;    /* height */
  499.     else
  500.         Nw.Height = GfxBase->DefaultFont->tf_YSize + 3;
  501.  
  502.     /*Nw.Width  = 20 + 5*8 + strlen(ep->Name) * (win->RPort->TxWidth + win->RPort->
  503. TxSpacing);*/
  504.  
  505.     itxt.IText = ep->Name;
  506.  
  507.     /* pretending spaces are always 8 */
  508.     Nw.Width  = 20 + 5*8 + IntuiTextLength(&itxt);        /* width */
  509.  
  510.  
  511.     Nw.LeftEdge= ep->IWinx;
  512.     Nw.TopEdge = ep->IWiny;
  513.  
  514.     if (Nw.LeftEdge + Nw.Width > win->WScreen->Width)   /* keep in bounds */
  515.         Nw.LeftEdge = win->WScreen->Width - Nw.Width;
  516.  
  517.     if (Nw.TopEdge + Nw.Height > win->WScreen->Height)
  518.         Nw.TopEdge = win->WScreen->Height - Nw.Height;
  519.  
  520.     Nw.Title = ep->Wtitle;
  521.  
  522.     Nw.Flags &= ~(WINDOWSIZING|WINDOWDEPTH|ACTIVATE);
  523.  
  524.     if (ep->Modified) {    /* no CLOSE */
  525.         Nw.Width -= 3*8;
  526.         Nw.Flags &= ~WINDOWCLOSE;
  527.     }
  528.  
  529.        /******* Nw.Flags |= BORDERLESS;*******/
  530.  
  531.     Nw.DetailPen = ep->BGPen;
  532.     Nw.BlockPen  = ep->FGPen;
  533.     if (win->Flags & WINDOWACTIVE)        /*    KTS */
  534.         Nw.Flags |= ACTIVATE;
  535.     sprintf(ep->Wtitle, "%s     ", ep->Name);
  536.  
  537.     if (newwin = opensharedwindow(&Nw)) {
  538.         closesharedwindow(win);
  539.         Nw.BlockPen = -1;
  540.         ep->iconmode = 1;
  541.         ep->Win = newwin;
  542.     }
  543.  
  544.     Nw.Flags |= WINDOWSIZING|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE;
  545.     Nw.Flags &= ~BORDERLESS;
  546.     }
  547. }
  548.  
  549.  
  550. #ifdef NOTDEF        /*    old iconify routine */
  551. void
  552. iconify()
  553. {
  554.     WIN *newwin;
  555.     ED *ep = Ep;
  556.     WIN *win = ep->Win;
  557.  
  558.     if (!ep->iconmode) {
  559.     ep->Winx      = win->LeftEdge;
  560.     ep->Winy      = win->TopEdge;
  561.     ep->Winwidth  = win->Width;
  562.     ep->Winheight = win->Height;
  563.     Nw.Height = win->RPort->TxHeight + 3;
  564.     Nw.Width  = 20 + 5*8 + strlen(ep->Name) * (win->RPort->TxWidth + win->RPort->TxSpacing);
  565.     if (ep->Modified)
  566.         Nw.Width -= 3*8;
  567.     Nw.LeftEdge= ep->IWinx;
  568.     Nw.TopEdge = ep->IWiny;
  569.     if (Nw.LeftEdge + Nw.Width > win->WScreen->Width)
  570.         Nw.LeftEdge = win->WScreen->Width - Nw.Width;
  571.     if (Nw.TopEdge + Nw.Height > win->WScreen->Height)
  572.         Nw.TopEdge = win->WScreen->Height - Nw.Height;
  573.     Nw.Title = ep->Wtitle;
  574.     Nw.Flags &= ~(WINDOWSIZING|WINDOWDEPTH|ACTIVATE);
  575.     if (ep->Modified)
  576.         Nw.Flags &= ~WINDOWCLOSE;
  577.     Nw.Flags |= BORDERLESS;
  578.     Nw.DetailPen = ep->BGPen;
  579.     Nw.BlockPen  = ep->FGPen;
  580.     if (win->Flags & WINDOWACTIVE)        /*    KTS */
  581.         Nw.Flags |= ACTIVATE;
  582.     sprintf(ep->Wtitle, "%s     ", ep->Name);
  583.     if (newwin = opensharedwindow(&Nw)) {
  584.         closesharedwindow(win);
  585.         Nw.BlockPen = -1;
  586.         ep->iconmode = 1;
  587.         ep->Win = newwin;
  588.     }
  589.     Nw.Flags |= WINDOWSIZING|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE;
  590.     Nw.Flags &= ~BORDERLESS;
  591.     }
  592. }
  593. #endif
  594.  
  595. void
  596. uniconify()
  597. {
  598.     ED *ep = Ep;
  599.     WIN *win = ep->Win;
  600.     WIN *newwin;
  601.     RP *rp;
  602.  
  603.     if (ep->iconmode) {
  604.     ep->IWinx = win->LeftEdge;
  605.     ep->IWiny = win->TopEdge;
  606.     Nw.LeftEdge = ep->Winx;
  607.     Nw.TopEdge  = ep->Winy;
  608.     Nw.Width    = ep->Winwidth;
  609.     Nw.Height   = ep->Winheight;
  610.     Nw.Title    = ep->Wtitle;
  611.     Nw.DetailPen = ep->BGPen;
  612.     Nw.BlockPen  = ep->FGPen;
  613.  
  614.     if (newwin = opensharedwindow(&Nw)) {
  615.         closesharedwindow(win);
  616.         win= ep->Win = newwin;
  617.         rp = win->RPort;
  618.  
  619.         menu_strip(win);
  620.         if (ep->Font)
  621.         SetFont(rp, ep->Font);
  622.         set_window_params();
  623.         if (!text_sync())
  624.         text_redisplay();
  625.         /** text_cursor(1); **/
  626.         MShowTitle = 0;
  627.         window_title();
  628.         ep->iconmode = 0;
  629.     }
  630.     }
  631. }
  632.  
  633.  
  634. void
  635. do_newwindow()
  636. {
  637.     WIN *win;
  638.  
  639.     if (Ep)
  640.     text_sync();
  641.     Nw.Title = (ubyte *)"    OK    ";
  642.  
  643.     if (text_init(Ep, NULL, &Nw)) {
  644.     if (win = opensharedwindow(&Nw)) {
  645.         menu_strip(win);
  646.         Ep->Win = win;
  647.         set_window_params();
  648.         text_load();
  649.     } else {
  650.         text_uninit();
  651.     }
  652.     }
  653. }
  654.  
  655. /*
  656.  *  openwindow with geometry specification.  Negative number specify
  657.  *  relative-right / relative-left (leftedge & topedge), or relative-width /
  658.  *  relative height (width & height).
  659.  *
  660.  *    <leftedge><topedge><width><height>
  661.  *
  662.  *  Example:    +10+10-20-20    Open window centered on screen 10 pixels
  663.  *                from the border on all sides.
  664.  */
  665.  
  666. void
  667. do_openwindow()
  668. {
  669.     WIN *win;
  670.  
  671.     if (Ep)
  672.     text_sync();
  673.     Nw.Title = (ubyte *)"    OK    ";
  674.  
  675.     if (text_init(Ep, NULL, &Nw)) {
  676.     GeometryToNW(av[1], &Nw);
  677.     if (win = opensharedwindow(&Nw)) {
  678.         menu_strip(win);
  679.         Ep->Win = win;
  680.         set_window_params();
  681.         text_load();
  682.     } else {
  683.         text_uninit();
  684.     }
  685.     }
  686. }
  687.  
  688.  
  689. WIN *
  690. TOpenWindow(nw)
  691. struct NewWindow *nw;
  692. {
  693.     WIN *win;
  694.  
  695.     while ((win = OpenWindow(nw)) == NULL) {
  696.     if (nw->Width < 50 || nw->Height < 50)
  697.         break;
  698.     nw->Width -= 10;
  699.     nw->Height-= 10;
  700.     }
  701.     return(win);
  702. }
  703.  
  704.  
  705. WIN *
  706. opensharedwindow(nw)
  707. struct NewWindow *nw;
  708. {
  709.     WIN *win;
  710.  
  711.     if (Sharedport)
  712.     nw->IDCMPFlags = NULL;
  713.     else
  714.     nw->IDCMPFlags = IDCMPFLAGS;
  715.     win = TOpenWindow(nw);
  716.     if (win) {
  717.     long xend = win->Width - win->BorderRight - 1;
  718.     long yend = win->Height- win->BorderBottom - 1;
  719.     if (Sharedport) {
  720.         win->UserPort = Sharedport;
  721.         ModifyIDCMP(win, IDCMPFLAGS);
  722.     } else {
  723.         Sharedport = win->UserPort;
  724.     }
  725.     ++Sharedrefs;
  726.     if (xend > win->BorderLeft && yend > win->BorderTop) {
  727.         SetAPen(win->RPort, nw->DetailPen);
  728.         RectFill(win->RPort, win->BorderLeft, win->BorderTop, xend, yend);
  729.         SetAPen(win->RPort, nw->BlockPen);
  730.     }
  731.     }
  732.     return(win);
  733. }
  734.  
  735. void
  736. closesharedwindow(win)
  737. WIN *win;
  738. {
  739.     static WIN *wunlink;
  740.     char notoktoclosenow = 0;
  741.  
  742.     if (win) {
  743.     SetWindowTitles(win, "", (char *)-1);
  744.     ClearMenuStrip(win);
  745.     Forbid();
  746.     win->UserPort = NULL;
  747.     ModifyIDCMP(win, GADGETUP);    /* NEVER occurs */
  748.  
  749.     notoktoclosenow = 1;
  750.  
  751.     Permit();
  752.     if (notoktoclosenow) {
  753.         win->UserData = (char *)wunlink;
  754.         wunlink = win;
  755.     } else {
  756.         CloseWindow(win);
  757.     }
  758.     --Sharedrefs;
  759.     } else {
  760.     if (Sharedrefs == 0 && Sharedport) {
  761.         DeletePort(Sharedport);
  762.         Sharedport = NULL;
  763.     }
  764.     for (win = wunlink; win; win = wunlink) {
  765.         wunlink = (WIN *)win->UserData;
  766.         CloseWindow(win);
  767.     }
  768.     wunlink = NULL;
  769.     }
  770. }
  771.  
  772.  
  773. getyn(text)
  774. char *text;
  775. {
  776.     int result;
  777.     ITEXT *body, *pos, *neg;
  778.  
  779.     body = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  780.     pos  = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  781.     neg  = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  782.     clrmem(body, sizeof(ITEXT));
  783.     clrmem(pos , sizeof(ITEXT));
  784.     clrmem(neg , sizeof(ITEXT));
  785.     body->BackPen = pos->BackPen = neg->BackPen = 1;
  786.     body->DrawMode= pos->DrawMode= neg->DrawMode= AUTODRAWMODE;
  787.     body->LeftEdge = 10;
  788.     body->TopEdge  = 12;
  789.     body->IText    = (ubyte *)text;
  790.     pos->LeftEdge = AUTOLEFTEDGE;
  791.     pos->TopEdge = AUTOTOPEDGE;
  792.     pos->IText = (ubyte *)"OK";
  793.     neg->LeftEdge = AUTOLEFTEDGE;
  794.     neg->TopEdge = AUTOTOPEDGE;
  795.     neg->IText = (ubyte *)"CANCEL";
  796.     result = AutoRequest(Ep->Win,body,pos,neg,0,0,320,58);
  797.     FreeMem(body, sizeof(ITEXT));
  798.     FreeMem(pos , sizeof(ITEXT));
  799.     FreeMem(neg , sizeof(ITEXT));
  800.     return(result);
  801. }
  802.  
  803. void
  804. title(buf)
  805. char *buf;
  806. {
  807.     SetWindowTitles(Ep->Win, buf, (char *)-1);
  808.     Oldtlen = 999;
  809.     MShowTitle = 3;
  810. }
  811.  
  812. void
  813. window_title()
  814. {
  815.     int len, maxlen;
  816.  
  817.     if (memoryfail) {
  818.     title(" -- NO MEMORY -- ");
  819.     memoryfail = 0;
  820.     text_redisplay();
  821.     }
  822.     if (MForceTitle) {
  823.     MShowTitle = 0;
  824.     MForceTitle = 0;
  825.     }
  826.     if (MShowTitle) {
  827.     --MShowTitle;
  828.     return;
  829.     }
  830.     {
  831.     char *mod;
  832.     FONT *oldfont;
  833.     ED *ep = Ep;
  834.     WIN *win = ep->Win;
  835.     RP *rp = win->RPort;
  836.  
  837.     mod = (ep->Modified) ? " (modified)" : "          ";
  838.     sprintf(ep->Wtitle, "%3ld/%-3ld %3ld %s%s  ",
  839.         text_lineno(),
  840.         text_lines(),
  841.         text_colno()+1,
  842.         text_name(),
  843.         mod
  844.     );
  845.     if (!text_imode())
  846.         strcat(ep->Wtitle, "Ovr ");
  847.     len = strlen(ep->Wtitle);
  848.     if (len < Columns && Columns < 128) {
  849.         setmem(ep->Wtitle+len, Columns - len + 1, ' ');
  850.         ep->Wtitle[Columns + 1] = 0;
  851.     }
  852.  
  853.     /*
  854.      *  Update title
  855.      */
  856.  
  857.     if (IntuitionBase->LibNode.lib_Version >= 36) {
  858.         SetWindowTitles(win, ep->Wtitle, (char *)-1);
  859.     } else {
  860.         oldfont = win->RPort->Font;
  861.         SetFont(rp, win->WScreen->RastPort.Font);
  862.  
  863.         win->Title = ep->Wtitle;
  864.         SetAPen(rp, ep->FGPen);
  865.         SetBPen(rp, ep->TPen);
  866.         Move(rp, 30, rp->Font->tf_Baseline+1);
  867.         maxlen = (win->Width-96)/rp->Font->tf_XSize;
  868.         if (maxlen < 0)
  869.         maxlen = 0;
  870.         if (len > maxlen)
  871.         len = Oldtlen = maxlen;
  872.         if (Oldtlen > maxlen)
  873.         Oldtlen = maxlen;
  874.         Text(rp, ep->Wtitle, len);        /*    No flash            */
  875.         while (Oldtlen - len >= (int)sizeof(Space)) {
  876.         Text(rp, Space, sizeof(Space));
  877.         Oldtlen -= sizeof(Space);
  878.         }
  879.         if (Oldtlen - len > 0)
  880.         Text(rp, Space, Oldtlen - len);
  881.         Oldtlen = len;            /*    Oldtlen might have been <   */
  882.         SetAPen(rp, ep->FGPen);
  883.         SetBPen(rp, ep->BGPen);
  884.         SetFont(rp, oldfont);
  885.     }
  886.     }
  887. }
  888.  
  889. void
  890. set_window_params()
  891. {
  892.     ED *ep = Ep;
  893.     WIN *win = ep->Win;
  894.     RP    *rp = win->RPort;
  895.  
  896.     Xsize = rp->Font->tf_XSize;
  897.     Ysize = rp->Font->tf_YSize;
  898.     Xbase = win->BorderLeft;
  899.     Ybase = win->BorderTop;
  900.     Xpixs   = win->Width - win->BorderRight - Xbase;
  901.     Ypixs   = win->Height- win->BorderBottom- Ybase;
  902.     Columns = Xpixs / Xsize;
  903.     Rows    = Ypixs / Ysize;
  904.     if (Columns <= 0)
  905.     Columns = 1;
  906.     if (Rows <= 0)
  907.     Rows = 1;
  908.     XTbase  =  Xbase;
  909.     YTbase  =  Ybase + rp->Font->tf_Baseline;
  910.     SetAPen(rp, ep->FGPen);
  911.     SetBPen(rp, ep->BGPen);
  912. }
  913.  
  914. void
  915. exiterr(str)
  916. char *str;
  917. {
  918.     if (Output()) {
  919.     Write(Output(),str,strlen(str));
  920.     Write(Output(),"\n",1);
  921.     }
  922.     exit(1);
  923. }
  924.  
  925.  
  926. /*
  927.  *  Check break by scanning pending messages in the I stream for a ^C.
  928.  *  Msgchk forces a check, else the check is only made if the signal is
  929.  *  set in the I stream (the signal is reset).
  930.  */
  931.  
  932. breakcheck()
  933. {
  934.     IMESS *im;
  935.     WIN *win = Ep->Win;
  936.     struct List *list = &win->UserPort->mp_MsgList;
  937.  
  938.     if (Msgchk || (SetSignal(0,0) & (1<<win->UserPort->mp_SigBit))) {
  939.     Msgchk = 0;
  940.     SetSignal(0,1<<win->UserPort->mp_SigBit);
  941.  
  942.     im = (IMESS *)list->lh_Head;
  943.     Forbid();
  944.     for (; im != (IMESS *)&list->lh_Tail; im = (IMESS *)im->ExecMessage.mn_Node.ln_Succ) {
  945.         if (im->Class == RAWKEY && (im->Qualifier & 0xFB) == 0x08 &&
  946.         im->Code == CtlC) {
  947.  
  948.         Permit();
  949.         SetSignal(SIGBREAKF_CTRL_C,SIGBREAKF_CTRL_C);
  950.         return(1);
  951.         }
  952.     }
  953.     Permit();
  954.     }
  955.     return(0);
  956. }
  957.  
  958. void
  959. breakreset()
  960. {
  961.     SetSignal(0, SIGBREAKF_CTRL_C);
  962. }
  963.  
  964. /*
  965.  *  resize cols rows
  966.  */
  967.  
  968. void
  969. do_resize()
  970. {
  971.     WIN *win = Ep->Win;
  972.     int cols = atoi(av[1]);
  973.     int rows = atoi(av[2]);
  974.     short width = (cols*win->RPort->Font->tf_XSize) + win->BorderLeft + win->BorderRight;
  975.     short height= (rows*win->RPort->Font->tf_YSize) + win->BorderTop + win->BorderBottom;
  976.  
  977.     if (width < 16 || height < 16 ||
  978.     width > win->WScreen->Width - win->LeftEdge ||
  979.     height > win->WScreen->Height - win->TopEdge) {
  980.     title ("window too big (try moving to upper left corner and retrying)");
  981.     return;
  982.     }
  983.     SizeWindow(win, width - win->Width, height - win->Height);
  984.     Delay(50*2);    /* wait 2 seconds */
  985. }
  986.  
  987. ops(av, iswb)
  988. char **av;
  989. {
  990.     short nonops;
  991.     short i;
  992.     long val;
  993.     char *str;
  994.  
  995.     if (av == NULL)
  996.     return(0);
  997.     for (i = nonops = 0; str = av[i]; ++i) {
  998.     if (iswb) {
  999.         if (strncmp(str, "ARG", 3) == 0) {
  1000.         while (*str && *str != '-')
  1001.             ++str;
  1002.         }
  1003.     }
  1004.     if (*str == '-') {
  1005.         val = atoi(str+2);
  1006.         switch(str[1]) {
  1007.         case 'f':
  1008.         Ffile = str+2;
  1009.         break;
  1010.         case 'b':
  1011.         /*SizeOveride = 1;*/
  1012.         break;
  1013.         case 't':
  1014.         /*Nwtopedge = val;*/
  1015.         break;
  1016.         case 'l':
  1017.         /*Nwleftedge= val;*/
  1018.         break;
  1019.         case 'w':
  1020.         /*SizeOveride = 1;*/
  1021.         /*Nwwidth   = val;*/
  1022.         break;
  1023.         case 'h':
  1024.         /*SizeOveride = 1;*/
  1025.         /*Nwheight  = val;*/
  1026.         break;
  1027.         }
  1028.     } else {
  1029.         ++nonops;
  1030.     }
  1031.     }
  1032.     return((int)nonops);
  1033. }
  1034.  
  1035. /*
  1036.  *  Convert geometry to nw params.
  1037.  */
  1038.  
  1039. char *
  1040. geoskip(ptr, pval, psgn)
  1041. char *ptr;
  1042. int *pval;
  1043. int *psgn;
  1044. {
  1045.     if (*ptr == '-')
  1046.     *psgn = -1;
  1047.     else
  1048.     *psgn = 1;
  1049.     if (*ptr == '-' || *ptr == '+')
  1050.     ++ptr;
  1051.     *pval = atoi(ptr);
  1052.     while (*ptr >= '0' && *ptr <= '9')
  1053.     ++ptr;
  1054.     return(ptr);
  1055. }
  1056.  
  1057. void
  1058. GeometryToNW(geo, nw)
  1059. char *geo;
  1060. struct NewWindow *nw;
  1061. {
  1062.     int n;
  1063.     int sign;
  1064.     struct Screen scr;
  1065.  
  1066.     GetScreenData(&scr, sizeof(scr), WBENCHSCREEN, NULL);
  1067.  
  1068.     if (*geo) {
  1069.     geo = geoskip(geo, &n, &sign);
  1070.     if (sign > 0)
  1071.         nw->LeftEdge = n;
  1072.     else
  1073.         nw->LeftEdge = scr.Width - n;
  1074.     }
  1075.     if (*geo) {
  1076.     geo = geoskip(geo, &n, &sign);
  1077.     if (sign > 0)
  1078.         nw->TopEdge = n;
  1079.     else
  1080.         nw->TopEdge = scr.Height - n;
  1081.     }
  1082.     if (*geo) {
  1083.     geo = geoskip(geo, &n, &sign);
  1084.     if (sign > 0)
  1085.         nw->Width = n;
  1086.     else
  1087.         nw->Width = scr.Width - nw->LeftEdge - n;
  1088.     }
  1089.     if (*geo) {
  1090.     geo = geoskip(geo, &n, &sign);
  1091.     if (sign > 0)
  1092.         nw->Height = n;
  1093.     else
  1094.         nw->Height = scr.Height - nw->TopEdge - n;
  1095.     }
  1096. }
  1097.  
  1098.  
  1099.